package archivoConfiguracionXML.logica;

import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class CreaXML {

	
	Document document;
	
	 public CreaXML()
	{
		 crearXml();
	}
	
	public  String mostrar() {
		
		String xml = "";
		try {
			XMLOutputter outputter1 = new XMLOutputter(Format.getPrettyFormat());
			xml = outputter1.outputString(document);
		} catch (Exception e) {
			System.out.println(e);
		}
		return xml;
	}

	public  void agregarCampoValor(String campo, String valor) {
		
		Element ele = new Element(campo);
		ele.addContent(valor);
		document.getRootElement().addContent(ele);
	}

	public  void generaArchivoXML() {
		XMLOutputter outputter1 = new XMLOutputter(Format.getPrettyFormat());
		try {
			FileWriter fw = new FileWriter("Empresa.xml");
			outputter1.output(document, fw);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private  String crearXml() {
		String xml = "";
		String XmlFile = "<Empresa>"

		+ "</Empresa>";
		SAXBuilder builder = new SAXBuilder();
		try {
			document = builder.build(new ByteArrayInputStream(XmlFile
					.getBytes()));
			XMLOutputter outputter1 = new XMLOutputter(Format.getPrettyFormat());
			xml = outputter1.outputString(document);
		} catch (Exception e) {
			System.out.println(e);
		}
		return xml;
	}
}
